package cn.sunline.test.account;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.sunline.test.account.model.Account;

@SpringBootApplication
@EnableDiscoveryClient
@RequestMapping("/account")
public class AccountApplication {
	private static final Logger log = LoggerFactory.getLogger(AccountApplication.class);
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public static void main(String[] args) {
		SpringApplication.run(AccountApplication.class, args);
	}

	@PostMapping
	@ResponseBody
	public Account add(@RequestBody Account account) {
		log.info("account info: " + account.toString());
		jdbcTemplate.update("INSERT INTO account(user_id, user_name, create_time) " 
				+ "values(?, ?, ?)",account.getUser_id(),account.getUser_name(),new Date());
		return jdbcTemplate.queryForObject("select * from account where user_id=?", new Object[]{account.getUser_id()},
				new BeanPropertyRowMapper<>(Account.class));
	}
}
